<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="产品编号" prop="productNumber">
        <el-input
          v-model="queryParams.productNumber"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="业务员" prop="salesperson">
        <el-input
          v-model="queryParams.salesperson"
          placeholder="请输入业务员"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="时间选择" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div style="height: 50px">
      <el-button v-hasPermi="['orderReport:orderCostProject:export']" type="primary" :loading="exportLoading" @click="handleExport">
        数据导出
      </el-button>
      <el-button @click="bulkEdit" v-hasPermi="['orderReport:orderCostProject:update']" type="primary"> 批量编辑 </el-button>
    </div>
    <el-table
      ref="multipleTableRef"
      border
      :height="tableHeight"
      v-loading="loading"
      :data="list"
      row-key="id"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        align="center"
        :reserve-selection="true"
        type="selection"
        fixed="left"
        width="80"
      />
      <el-table-column label="序号" align="center" type="index" fixed="left" width="80" />
      <el-table-column
        label="产品编号"
        header-align="left"
        align="left"
        prop="productNumber"
        min-width="200px"
      />
      <el-table-column
        label="客户代码"
        header-align="left"
        align="left"
        prop="customerCode"
        min-width="200px"
      />
      <el-table-column
        label="客户名称"
        header-align="left"
        align="left"
        prop="customerName"
        min-width="200px"
      />
      <el-table-column
        label="业务员"
        header-align="left"
        align="left"
        prop="salesperson"
        min-width="200px"
      />
      <el-table-column
        label="产品名称"
        header-align="left"
        align="left"
        prop="productName"
        min-width="200px"
      />
      <el-table-column
        label="产品规格"
        header-align="left"
        align="left"
        prop="productStandard"
        min-width="200px"
      />
      <el-table-column
        label="创建时间"
        header-align="left"
        align="left"
        prop="createTime"
        min-width="200px"
      />
      <el-table-column label="订单类型" header-align="left" align="left" min-width="200px">
        <template #default="scope">
          <dict-tag :type="'market_order_type'" :value="scope.row.dictOrderType" />
        </template>
      </el-table-column>
      <el-table-column label="制造成本预估（不含税）" align="center" width="220">
        <el-table-column
          label="主材"
          header-align="right"
          align="left"
          prop="principalMaterial"
          min-width="200px"
        />
        <el-table-column
          label="辅材"
          header-align="right"
          align="left"
          prop="auxiliaryMaterial"
          min-width="200px"
        />
        <el-table-column
          label="外协"
          header-align="right"
          align="left"
          prop="outsourcing"
          min-width="200px"
        />
        <el-table-column
          label="运费"
          header-align="right"
          align="left"
          prop="freight"
          min-width="200px"
        />
        <el-table-column
          label="电气"
          header-align="right"
          align="left"
          prop="electric"
          min-width="200px"
        />
        <el-table-column
          label="合计"
          header-align="right"
          align="left"
          prop="totalPrice"
          min-width="200px"
        />
      </el-table-column>
      <el-table-column label="状态" header-align="left" align="left" min-width="200px">
        <template #default="scope">
          <dict-tag :type="'dict_cost_status'" :value="scope.row.dictCostStatus" />
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        header-align="left"
        align="left"
        prop="remark"
        min-width="200px"
      />
      <el-table-column label="操作" header-align="left" align="left" width="120" fixed="right">
        <template #default="scope">
          <el-button link type="primary" @click="openForm(scope.row,'update')" v-hasPermi="['orderReport:orderCostProject:update']">
            编辑
          </el-button>
          <el-button link type="primary" @click="openForm(scope.row,'view')" >
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
</template>

<script setup lang="ts" name="OrderCost">
  import download from '@/utils/download'
  import * as OrderCostProjectApi from '@/api/reportQuery/orderReport/orderCostProject'
  const message = useMessage() // 消息弹窗
  const { t } = useI18n() // 国际化
  const multipleTableRef = ref()
  const loading = ref(true) // 列表的加载中
  const total = ref(0) // 列表的总页数
  const list = ref([]) // 列表的数据
  const { push } = useRouter()
  const queryParams = reactive({
    pageNo: 1,
    pageSize: 50,
    productNumber: null,
    productName: null,
    customerName: null,
    salesperson: null,
    createTime: undefined
  })
  const queryFormRef = ref() // 搜索的表单
  const exportLoading = ref(false) // 导出的加载中

  /** 查询列表 */
  const getList = async () => {
    loading.value = true
    try {
      const data = await OrderCostProjectApi.getOrderCostPage(queryParams)
      list.value = data.list
      total.value = data.total
    } finally {
      loading.value = false
    }
  }

  /** 搜索按钮操作 */
  const handleQuery = () => {
    queryParams.pageNo = 1
    getList()
  }

  /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value.resetFields()
    handleQuery()
  }
  // 编辑
  const openForm = (row,type) => {
    let arr = ref<any>([])
    arr.value.push(row)
    let params = {
      data: arr.value
    }
    push({
      path: '/reportQuery/reportQuery/orderReport/orderCostProjectForm',
      query: {
        data: JSON.stringify(params),
        type:type
      }
    })
  }
  //批量编辑
  const bulkEdit = () => {
    console.log(selectList.value.length)
    if (selectList.value.length) {
      let params = {
        data: selectList.value
      }
      push({
        path: '/reportQuery/reportQuery/orderReport/orderCostProjectForm',
        query: {
          data: JSON.stringify(params),
          type:'update'
        }
      })
    } else {
      message.error(t('请选择要编辑的数据'))
    }
  }

  /**选中 */
  const selectList = ref<any[]>([])
  const handleSelectionChange = (data) => {
    selectList.value = data
  }
  /** 导出按钮操作 */
  // const handleExport = async () => {
  //   try {
  //     // 导出的二次确认
  //     await message.exportConfirm()
  //     // 发起导出
  //     exportLoading.value = true
  //     const data = await OrderCostProjectApi.exportOrderCost(queryParams)
  //     download.excel(data, '订单管理-订单报表-成本估算表(项目技术室).xls')
  //   } catch {
  //   } finally {
  //     exportLoading.value = false
  //   }
  // }

  /** 导出按钮操作 */
  const exportRef = ref()
  const handleExport = () => {
    exportRef.value.open()
  }
  // 导出
  const downloadFun = async () => {
    const val = exportRef.value.params
    let params = {
      fileName: val.name,
      ids: [] as any
    }
    if (val.range === 'all') {
      params.ids = []
    } else {
      params.ids = selectList.value.map((item) => {
        return item['id']
      })
    }
    OrderCostProjectApi.exportOrderCost(params).then((res) => {
      download.excel(res, val.name)
      clearSelectData()
    })
  }

  //清空多选
  const clearSelectData = () => {
    selectList.value = []
    multipleTableRef.value!.clearSelection()
  }

  /** 初始化 **/
  /** 初始化 **/
  const tableHeight = ref(0)
  onMounted(async () => {
    const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
    tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
    getList()
  })
</script>
